import {
	wxLogin,
	socialLogin,
	getUser,
	logout,
	smsLogin
} from '@/apis/user.js'
import store from "@/store/index.js"
let uDate = uni.getStorageSync("UserData");
let uInfo = uni.getStorageSync("UserInfo");
//token数据
if (!uInfo || uDate.length == 0) {
	uDate = null;
}
export let userData = uDate;

//userInfo数据
if (!uInfo || uInfo.length == 0) {
	uInfo = null
} else {
	uInfo = uInfo
}

store.commit("setUserInfo", uInfo);

export let userInfo = uInfo;
// 微信小程序手机号授权登陆
export const mobileLogin = async (e) => {

	return new Promise(async (resolve, reject) => {
		if (e.errMsg !== 'getPhoneNumber:ok') {
			return resolve(false);
		}
		let data = {
			phoneCode: e.code, // e.code,
			loginCode: '', //codeResult.code,
			state: 'default'
		}
		// 1. 获得微信 code

		const codeResult = await uni.login();

		if (typeof codeResult == 'object') {
			if (Array.isArray(codeResult)) {
				codeResult.forEach(x => {
					if (x && x.errMsg == 'login:ok') {
						data.loginCode = x.code
					}
				})
			} else {
				if (codeResult.errMsg == 'login:ok') {
					data.loginCode = codeResult.code
				}
			}

		}
		data.code = data.loginCode
		if (data.loginCode == '') {
			return resolve(false);
		}
		// 2. 一键登录
		const loginResult = await wxLogin(data);
		if (loginResult.code === 0) {
			setOpenid(loginResult.data.openid);
			userData = new UserData(loginResult.data)
			setUserData(userData)
			return resolve(true);
		} else {
			return resolve(false);
		}
	});
};


// 手机号验证码登陆
export const phoneLogin = async (e) => {
	return new Promise(async (resolve, reject) => {
		const loginResult = await smsLogin({
			mobile:e.mobile,
			code:e.code	
		})
		// 2. 一键登录
		if (loginResult.code === 0) {
			setOpenid(loginResult.data.openid);
			userData = new UserData(loginResult.data)
			setUserData(userData)
			return resolve(true);
		} else {
			uni.showToast({
				icon:"none",
				title:loginResult.msg
			})
			return resolve(false);
		}
	});
};

export const getUserInfo = () => {
	return new Promise(async (r, rej) => {
		if (store.state.state != null) {
			r(store.state.state)
		} else {
			if (checkUserData()) {
				let res = await getUser();
				if (res.code == 0) {
					userInfo = Object.assign(userData.data, res.data);
				}
				uni.setStorageSync("UserInfo", userInfo)
				store.commit("setUserInfo", userInfo);
				r(userInfo)
			}
			r(null)
		}
	})
}

export const refreshUserInfo = async () => {
	if (checkUserData()) {
		let res = await getUser();
		if (res.code == 0) {
			userInfo = Object.assign(userData.data, res.data);
		}
		uni.setStorageSync("UserInfo", userInfo)
		store.commit("setUserInfo", userInfo);
	}
}

export const userLogout = async () => {
	await logout();
	userData = null;
	userInfo = null;
	uni.removeStorageSync("UserData");
	uni.removeStorageSync("UserInfo");
	store.commit("setUserInfo", null);
}


export const wxSocialLogin = async () => {
	let data = {
		code: '',
		state: 'default'
	}
	// 1. 获得微信 code
	const codeResult = await uni.login();

	if (typeof codeResult == 'object') {
		if (Array.isArray(codeResult)) {
			codeResult.forEach(x => {
				if (x && x.errMsg == 'login:ok') {
					data.code = x.code
				}
			})
		} else {
			if (codeResult.errMsg == 'login:ok') {
				data.code = codeResult.code
			}
		}

	}

	const loginResult = await socialLogin(data);

	if (loginResult.code === 0) {
		setOpenid(loginResult.data.openid);
		userData = new UserData(loginResult.data)
		setUserData(userData)
		return true;
	} else {
		return false;
	}
};


function setOpenid(openid) {
	uni.setStorageSync('openid', openid);
}

function getOpenId() {
	return uni.getStorageSync('openid')
}


export function setUserData(data) {
	userData = data;
	uni.setStorageSync('UserData', data);
}


function getUserData() {
	let JsonData = uni.getStorageSync('UserData')

	return JsonData
}


export function checkUserData() {
	if (userData) {
		return true
	} else {
		let data = getUserData()
		if (data) {
			userData = data;
			return true
		}
	}
	return false
}

export class UserData {
	data;
	nickName = '';
	avatarUrl = '';
	phone = ''
	name = ''
	sex = ''
	birthday = ''
	profession = ''
	usedRoomData = {}
	toPage = ''
	ReloginFun = () => {}
	//  GS || TZ || GLY
	memberType = 'GS'
	constructor(data) {
		this.data = data;
	}
}



export let homePageTopInfo = {
	id: 0,
	type: 'image',
	title: 'xx直播间',
	name: '周二、周三、周四',
	hot: '2h/档',
	share: '216',
	love: '962',
	avatar: 'https://resource.tuniaokj.com/images/blogger/blogger_beibei.jpg',
	img: 'https://xiaozhushou.obs.cn-south-1.myhuaweicloud.com:443/miniApp/pic/topPic1.png?AccessKeyId=C3BURFTE5HVLX7QXFZ3A&Expires=1721837783&Signature=JcREigP9aIya7Mx/WWMbBBib9Zo%3D'
}

export let resumeList = [{
	id: 0,
	type: 'image',
	title: 'xx直播间',
	name: '周二、周三、周四',
	hot: '2h/档',
	share: '216',
	love: '962',
	avatar: 'https://resource.tuniaokj.com/images/blogger/blogger_beibei.jpg',
	url: 'https://resource.tuniaokj.com/images/resume/resume-bg.jpg',
}]

export let roomData = [{
		title: '直播间1',
		icon: 'image-text-fill',
		color: '#5177EE',
		value: '前往咨询'
	},
	{
		title: '直播间2',
		icon: 'data-fill',
		color: '#5177EE',
		value: '前往咨询'
	},
	{
		title: '直播间3',
		icon: 'statistics-fill',
		color: '#5177EE',
		value: '前往咨询'
	},
	{
		title: '直播间4',
		icon: 'bankcard-fill',
		color: '#5177EE',
		value: '前往咨询'
	}
]